package org.nzj.useradmin.action;

/*
 * 2012-5-25 update ssh v1.0
 * @author <a href="mailto:nzj1981@gmail.com">nzj</a>
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;
import org.nzj.useradmin.dao.IUserDAO;
import org.nzj.useradmin.util.DateFormatTest;
import org.nzj.useradmin.util.IPTimeStamp;
import org.nzj.useradmin.vo.User;

import bsh.This;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport implements ModelDriven<User> {
	private String code;//获取校验码

	private List<File> file;// 封装上传文件

	private List<String> fileContentType;// 上传文件的类型

	private List<String> fileFileName;// 设置上传文件的文件名

	public String url = null; //获取返回页面

	private User user;//获取用户信息

	@Resource
	IUserDAO userDAO;
	public String getCode() {
		return code;
	}
	public List<File> getFile() {
		return file;
	}
	public List<String> getFileContentType() {
		return fileContentType;
	}
	public List<String> getFileFileName() {
		return fileFileName;
	}
	@Override
	public User getModel() {
		if (null == user) {
			return user = new User();
		}
		return user;
	}
	public User getUser() {
		return user;
	}

	/*
	 * @function 用户登陆
	 * 
	 * @return 返回相应页面
	 */
	public String login() throws Exception {

		/*
		 * user.setPasswd(this.getUser().getPasswd());
		 * user.setUserno(this.getUser().getUserno());
		 */
		String rand = (String) ActionContext.getContext().getSession().get(
				"rand1");
		if (this.getCode() == null || rand == null
				|| !rand.trim().equals(this.getCode().trim())) {
			this.addFieldError("code", "验证码不能为空或输入有误");
			this.url = "/error.jsp";
			return ERROR;
		}
		if ("".equals(this.getUser().getUserno().trim())
				|| "".equals(this.getUser().getPasswd().trim())
				|| null == this.getUser().getUserno()
				|| null == this.getUser().getPasswd()) {
			this.addFieldError("userno", "用户名或者密码为空");
			this.url = "/login.jsp";
			return INPUT;

		}
//		System.out.println(this.getUser());
		boolean u = userDAO.findLogin(this.getUser());
		if (u) {
			// 得到姓名放到session中
			ActionContext.getContext().getSession().put("name", user.getName());
			this.url = "/index.jsp";
			return SUCCESS;
		} else {
			this.addFieldError("userno", "用户名或者密码有错误!");
			this.url = "/login.jsp";
			return INPUT;
		}

	}

	/*
	 * @function 用户注销清除session
	 * 
	 * @return 返回到首页
	 */
	public String logout() throws Exception {
		// 清除session
		ActionContext.getContext().getSession().remove("name");
		this.url = "/logout.jsp";
		return SUCCESS;

	}

	/*
	 * @function reg()用户注册
	 * 
	 * @return 返回相应页面
	 */
	public String reg() throws Exception {

		// 获取ip地址
		String ip=ServletActionContext.getRequest().getRemoteAddr();
		getUser().setIp(ip);
		//获取注册时间
		String regTime= DateFormatTest.getStringFromDate(new Date(), "yyyyMMddHHmmss");
		getUser().setRegtime(regTime);
		//获取到头像并写入upload文件夹中
		
		//设置路径
		String path = ServletActionContext.getServletContext().getRealPath("upload");
		//判断upload文件夹是否存
		File file= new File(path);
		if (!file.exists()) {
		   file.mkdir();
		}
		//循环读取上传文件 
		String fileName=null;
		String newFileName=null;
		String fileExt=null;
		try {
			if (null!=this.getFile()) {
				for (int i = 0; i < this.getFile().size(); i++) {
					//文件名
					 fileName=this.getFileFileName().get(i);
					//得到文件类型
					fileExt=fileName.substring(fileName.lastIndexOf("."));
					//文件重新命名
					newFileName=new IPTimeStamp(ip).getIPTimeStampRand()+fileExt;
					//获取头像新路径及名称并入库
					getUser().setPic("upload\\"+newFileName);
					//得到文件对象
					File f = new File(path,newFileName);
					//文件输出流
					FileOutputStream os = new FileOutputStream(f);
					//文件输入流
					FileInputStream is = new FileInputStream(this.getFile().get(i));
					IOUtils.copy(is, os);
					/*byte[] buffer = new byte[(int)this.getFile().get(i).length()];
					int len=0;
					while ((len=is.read(buffer))>0){
						os.write(buffer,0,len);
					}*/
					os.close();
					is.close();
					
				}
				}
		} catch (Exception e) {
			e.printStackTrace();
			this.url = "/register.jsp";
			return INPUT;
		}
		//用户注册写入库中
		boolean s = userDAO.doCreate(getUser());
		if (s) {
			this.url = "/success.jsp";
			return SUCCESS;
		}else {
			this.addFieldError("userno", "用户名已存在");
			this.url = "/register.jsp";
			return INPUT;
		}
		
	
		
	}

	public void setCode(String code) {
		this.code = code;
	}

	public void setFile(List<File> file) {
		this.file = file;
	}

	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}

	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}

	public void setUser(User user) {
		this.user = user;
	}

}
